<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="130px">
			<el-form-item label="模板名称" prop="name">
				<el-input v-model="FormData.name" placeholder="请填写模板名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="合格分" prop="passScore">
				<el-input-number v-model="FormData.passScore" class="mx-4" :min="1" controls-position="right"
				 />
			</el-form-item>
			<el-form-item label="描述" prop="demo">
				<el-input v-model="FormData.demo" type="textarea" placeholder="请填写描述" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				fileurlArr: [],
				//表单数据
				FormData: {
					name: '',
					passScore: 1,
					demo: ''
				},
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请填写模板名称'
					}],
					passScore: [{
						required: true,
						message: '请填写合格分'
					}],
					demo: [{
						required: true,
						message: '请填写描述'
					}],

				},
				unitList: [],
				userInfo: {}
			}
		},
		created() {

		},
		mounted() {},
		methods: {

			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true

				delete this.FormData.createByName
				console.log(this.FormData)

				const res = await this.$API.api.ptemp.save.post(this.FormData)
				this.isSaveing = false;
				if (res.code == 200) {
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.FormData.id = data.id
				this.FormData.demo = data.demo
				this.FormData.name = data.name
				this.FormData.timeout = data.timeout
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.FormData, data)
			}
		}
	}
</script>

<style>
</style>
